<?php
/*
        Nodes
*/

class H2o_Node {
    var $position;
    function __construct($parser, $argstring) {}
    
    function render($context, $stream) {}
}

class Node_List extends H2o_Node implements IteratorAggregate  {
    var $parser;
    var $list;
    
    function __construct(&$parser, $initial = null, $position = 0) {
        $this->parser = $parser;
        if (is_null($initial))
            $initial = array();
        $this->list = $initial;
        $this->position = $position;
    }

    function compile($stream) {
        foreach($this->list as $node) {
            $node->compile($stream);
        }
    }
    
    function append($node) {
        array_push($this->list, $node);
    }

    function extend($nodes) {
        array_merge($this->list, $nodes);
    }

    function getLength() {
        return count($this->list);
    }
    
    public function getIterator() {
        return new ArrayIterator( $this->list );
    }
}

class Variable_Node extends H2o_Node {
    private $filters = false;
    var $variable;
    
    function __construct($variable, $filters, $position = 0) {
        if (!empty($filters))
            $this->filters = $filters;
        $this->variable = substr($variable, 1);
    }

    function compile($stream) {
        $code = "\$c['{$this->variable}']";
        if ($this->filters)
            $code = $this->applyFilters($code);

        $stream->write('<?='.$code.'?>');
    }
    
    function applyFilters($variable) {
        foreach($this->filters as $filter) {
            $variable = sprintf("%s(%s)", substr($filter, 1), $variable);
        }
        return $variable;
    }
}

class Comment_Node extends H2o_Node {}

class Text_Node extends H2o_Node {
    var $content;
    function __construct($content, $position = 0) {
        $this->content = $content;
        $this->position = $position;
    }
    
    function compile($stream) {
        $stream->write($this->content);
    }
    
    function is_blank() {
        return strlen(trim($this->content));
    }
}

class For_Node extends H2o_Node {
    public $position;
    private $iteratable, $key, $item, $body, $else, $limit, $reversed;
    private $syntax = '{
        ([a-zA-Z][a-zA-Z0-9-_]*)(?:,\s?([a-zA-Z][a-zA-Z0-9-_]*))?
        \s+in\s+
        ([a-zA-Z][a-zA-Z0-9-_]*(?:\.[a-zA-Z_0-9][a-zA-Z0-9_-]*)*)\s*   # Iteratable name
        (?:limit\s*:\s*(\d+))?\s*
        (reversed)?                                                     # Reverse keyword
    }x';

    function __construct($argstring, $parser, $position) {
        if (!preg_match($this->syntax, $argstring, $match))
            throw new Exception("Invalid for loop syntax ");
        
        $this->body = $parser->parse('endfor', 'else');
        
        if ($parser->token->content === 'else')
            $this->else = $parser->parse('endfor');

        @list(,$this->key, $this->item, $this->iteratable, $this->limit, $this->reversed) = $match;
        
        if ($this->limit)
            $this->limit = (int) $this->limit;

        # Swap value if no key found
        if (!$this->item) {
            list($this->key, $this->item) = array($this->item, $this->key);
        }
        $this->iteratable = $this->iteratable;
        $this->reversed = (bool) $this->reversed;
    }

    function compile($stream) {
        $bodystream = new StreamWriter;
        $this->body->compile($bodystream);
        $source = $this->template($this->iteratable, $this->item, $this->key, $bodystream->close());
        $stream->write($source);
    }
    
    function template($iteratable, $item, $index, $body) {
        if (!$index) $index = 'i';
        $source = <<<EOD
<?php foreach(\$c['{$iteratable}'] as \${$index} => \${$item}): \$c->push(compact('{$item}')); ?>
{$body}
<?php \$c->pop();endforeach ?>
EOD;
        return $source;
    }
}

?>